<template>


  <div>

    <el-row>


      <el-col :span="12">
        <el-button @click="showEdit = true">添加</el-button>
      </el-col>
    </el-row>


    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="typeId" label="typeId" width="180" />
      <el-table-column prop="type" label="type" width="180" />


      <el-table-column prop="caption" label="caption">
        <template #default="scope">
          <el-button link type="primary" @click="goToItemList(scope.row)">{{ scope.row.caption }} </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="createtime" label="createtime" />
      <el-table-column prop="userid" label="操作">
        <template #default="scope">
          <el-button link type="primary" @click="deleteById(scope.row.typeId)">删除</el-button>
          <el-button link type="primary" @click="toEdit(scope.row.typeId)">修改</el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination v-model:current-page="queryParam.current" :page-sizes="[3, 5, 10, 15, 20, 30, 40]"
      v-model:page-size="queryParam.size" size="default" layout="sizes, prev, pager, next" :total="total"
      @size-change="loadDataList" @current-change="loadDataList" />



    <el-dialog title="常数项管理" width="600px" v-model="showEdit">

      <el-form :model="form" label-width="auto">
        <el-form-item label="常数项代码">
          <el-input v-model="form.type" />
        </el-form-item>
        <el-form-item label="常数项类型名称">
          <el-input v-model="form.caption" />
        </el-form-item>


        <el-form-item>
          <el-button type="primary" @click="save">保存</el-button>
          <el-button @click="closeDialog()">Cancel</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>


  </div>



</template>


<script lang="ts" setup name="User">

import { ref, onMounted, reactive } from 'vue'
import { service } from "@/utils/request";
import { type PageInfo } from '@/utils/crud'
import { useRouter } from 'vue-router'

const tableData = ref<[]>()
const queryParam = reactive({
  current: 1,
  size: 10,
});
const total = ref(0);

const form = reactive({
  type: '',
  caption: '',
})
const showEdit = ref(false);

const router = useRouter();

interface ConstantsType {
  typeId?: string
  type: string,
  caption: string,
  createtime?: string
}

function loadDataList() {

  // 为给定 ID 的 user 创建请求
  //data  真实的数据
  service.get<PageInfo<ConstantsType>>('/constants_type/page', { params: queryParam }).then(function (page) {
    tableData.value = page.data;
    total.value = page.total;
  })

}

function deleteById(id: string) {
  service.get<boolean>('/constants_type/delete?id=' + id).then(function (success) {
    if (success) {
      alert("删除成功");
      loadDataList();
    } else {
      alert("删除失败")
    }

  })
}

function save() {

  console.log(form)
  service.get('/constants_type/saveOrUpdate', { params: form }).then(function (success) {
    if (success) {
      alert("保存成功");
      closeDialog();
      loadDataList();
    } else {
      alert("保存")
    }

  })
}


function toEdit(id: string) {
  showEdit.value = true;


  //根据主键查询 数据 放到  form 表单
  service.get('/constants_type/getById?id=' + id).then((vo) => {
    form.typeId = vo.typeId;
    form.type = vo.type;
    form.caption = vo.caption;

  })


}


function goToItemList(row: ConstantsType) {
  const type = row.type;
  const caption = row.caption;

  router.push({
    path: '/home/constants_item',
    query: {
      type: type,
      caption: caption
    }
  })


}


function closeDialog() {
  showEdit.value = false
}

onMounted(() => {
  // 获取数据
  loadDataList();

})


</script>
